package cn.controller;

import cn.config.C2_01_SignHelloWorld;
import cn.domain.Employee;
import cn.domain.TaskTableVo;
import cn.domain.Tasktable;
import cn.hutool.jwt.JWT;
import cn.service.TasktableService;
import com.bw.itext.pdf.service.PdfService;
import com.itextpdf.text.DocumentException;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
public class TaskController {
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
    @Autowired
    HistoryService historyService;
    @Autowired
    TasktableService tasktableService;

    @Autowired
    PdfService pdfService;
    @Autowired
    C2_01_SignHelloWorld c201SignHelloWorld;
    @Autowired
    KafkaTemplate kafkaTemplate;
    @RequestMapping("tadd")
    public  void tadd(@RequestBody Tasktable tasktable){
        boolean save = tasktableService.save(tasktable);
       if (save){
           HashMap<String, Object> map = new HashMap<>();
           map.put("person",tasktable.getName());
           ProcessInstance instance = runtimeService.startProcessInstanceByKey("year", tasktable.getId() + "", map);
                  System.err.println("实例id："+instance.getId());

       }
    }
    @RequestMapping("tlist")
    public  List<TaskTableVo> tlist(HttpServletRequest request){
        ArrayList<TaskTableVo> list = new ArrayList<>();
        String header = request.getHeader("token");
        JWT jwt = JWT.of(header);
        String name = jwt.getPayload("name").toString();

        List<Task> tasks = taskService.createTaskQuery().taskAssignee(name).list();

        for (Task task : tasks) {
            TaskTableVo taskTableVo = new TaskTableVo();
            taskTableVo.setTaskId(task.getId());
            taskTableVo.setTaskName(task.getName());
            taskTableVo.setAssignee(task.getAssignee());
            taskTableVo.setProcessInstanceId(task.getProcessInstanceId());
            HistoricProcessInstance instance = historyService.createHistoricProcessInstanceQuery()
                    .processInstanceId(task.getProcessInstanceId())
                    .singleResult();
            String businessKey =  instance.getBusinessKey();
            Tasktable byId = tasktableService.getById(businessKey);
             taskTableVo.setReason(byId.getReason());
             taskTableVo.setStartTime(new Date());
             taskTableVo.setDays(byId.getDays());
             taskTableVo.setId(byId.getId());
             list.add(taskTableVo);
        }
        return list; 
    }
    @RequestMapping("complete")
    public  void complete(@RequestBody TaskTableVo taskTableVo) throws IOException, GeneralSecurityException, DocumentException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("days",taskTableVo.getDays());
        map.put("flag",taskTableVo.getFlag());
        taskService.complete(taskTableVo.getTaskId(),map);
                System.err.println(taskTableVo.getTaskName());
        System.err.println(taskTableVo.getFlag());
        if ("人事审批".equals(taskTableVo.getTaskName())&&"1".equals(taskTableVo.getFlag())){
            List<Tasktable> list = tasktableService.list(taskTableVo.getId());
            pdfService.export(list,"task.pdf","员工详情", Tasktable.class);
            c201SignHelloWorld.create("D:\\pdf\\sign\\task.pdf","D:\\pdf\\sign\\tskSign.pdf","D:\\pdf\\sign\\sign.png");
            kafkaTemplate.send("first",taskTableVo+"通过").addCallback(
                    new ListenableFutureCallback() {
                        @Override
                        public void onFailure(Throwable throwable) {
                                    System.err.println("发送失败");
                        }

                        @Override
                        public void onSuccess(Object o) {
                            System.err.println("发送成功");
                        }
                    }
            );
        }
    }

}
